package com.sunyur.tailor.channel.service.outer.supplier.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.sunyur.tailor.channel.common.middle.base.ChannelResult;
import com.sunyur.tailor.channel.common.middle.enums.MiddleResultEnum;
import com.sunyur.tailor.channel.common.middle.util.RestTemplateUtil;
import com.sunyur.tailor.channel.service.outer.supplier.param.OuterSupplierParam;
import com.sunyur.tailor.channel.service.outer.supplier.service.OuterSupplierService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import java.util.Objects;
import java.util.Set;

/**
 *
 * @Author 99288
 * @Description 外部供应商实现类
 * @Date 16:50 2021/6/26
 * @Param
 * @return
**/
@Slf4j
@Service("outerSupplierService")
public class OuterSupplierServiceImpl implements OuterSupplierService {

    @Autowired
    private Validator validator;
    @Autowired
    RestTemplateUtil restTemplateUtil;

    @Value("${outer.oa.supplier}")
    private String supplierUrl;

    @Override
    public ChannelResult<Boolean> syncSupplier(OuterSupplierParam param) {
        ChannelResult<Boolean> result = new ChannelResult();
        result.setError(MiddleResultEnum.FAILED.getCode(), MiddleResultEnum.FAILED.getValue());
        //校验参数
        Set<ConstraintViolation<OuterSupplierParam>> validate = validator.validate(param);
        if(!CollectionUtils.isEmpty(validate)){
            StringBuffer errorMessage = new StringBuffer();
            validate.forEach($->errorMessage.append($.getMessage()));
            result.setError(MiddleResultEnum.PARAM_ERROR.getCode(),errorMessage.toString());
            return result;
        }
        //同步供应商基本信息接口
        JSONObject jsonObject = restTemplateUtil.exchangeJsonPost(param, supplierUrl);
        if(Objects.nonNull(jsonObject)){

        }
        return result;
    }
}
